package cn.micro.system.manage.service.impl;

import cn.micro.system.manage.entity.SysOdsLoginRecord;
import cn.micro.system.manage.entity.vo.SysLoginRecordQueryVo;
import cn.micro.system.manage.mapper.SysOdsLoginRecordMapper;
import cn.micro.system.manage.service.SysOdsLoginRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 访问系统记录 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-12-25
 */
@Service
public class SysOdsLoginRecordServiceImpl extends ServiceImpl<SysOdsLoginRecordMapper, SysOdsLoginRecord> implements SysOdsLoginRecordService {

    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {
        SysOdsLoginRecord sysOdsLoginRecord = new SysOdsLoginRecord();
        sysOdsLoginRecord.setUsername(username);
        sysOdsLoginRecord.setStatus(status);
        sysOdsLoginRecord.setIpaddr(ipaddr);
        sysOdsLoginRecord.setMsg(message);
        sysOdsLoginRecord.setAccessTime(LocalDateTime.now());
        this.baseMapper.insert(sysOdsLoginRecord);
    }

    @Override
    public List<SysOdsLoginRecord> selectPage(int page, int size, SysLoginRecordQueryVo sysLoginRecordQueryVo) {
        //创建page对象
        PageHelper.startPage(Integer.parseInt(String.valueOf(page)),Integer.parseInt(String.valueOf(size)));
        //获取条件值
        String username = sysLoginRecordQueryVo.getUsername();
        String createTimeBegin = sysLoginRecordQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginRecordQueryVo.getCreateTimeEnd();
        //封装条件
        LambdaQueryWrapper<SysOdsLoginRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(username),SysOdsLoginRecord::getUsername,username)
                .ge(!StringUtils.isEmpty(createTimeBegin),SysOdsLoginRecord::getCreateTime,createTimeBegin)
                .le(!StringUtils.isEmpty(createTimeBegin),SysOdsLoginRecord::getCreateTime,createTimeEnd);
        return this.baseMapper.selectList(queryWrapper);
    }
}
